<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>07.Controls(Camera)</title>
    <style>
        body {
            margin: 0;
        }
    </style>
</head>

<body>
    <script src="build/three.js"></script>
    <script>
        //生成场景
        const scene = new THREE.Scene();
        //设置相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 5;
        //设置渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        //生成模型
        const geometry = new THREE.BoxGeometry();
        const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
        const cube = new THREE.Mesh(geometry, material);
        //设置场景
        scene.add(cube);
        //键盘控制相机移动
        document.onkeydown = function (event) {
            if (event.altKey == false) {
                //没有按下alt键
                switch (event.key) {  // 获取当前按下键盘键的编码
                    //wsad移动
                    case 'w':
                        camera.position.z -= 0.5;
                        break;
                    case 's':
                        camera.position.z += 0.5;
                        break;
                    case 'a':
                        camera.position.x -= 0.5;
                        break;
                    case 'd':
                        camera.position.x += 0.5;
                        break;
                }
            }else
            {
                //按住alt键
                switch (event.keyCode) {  // 获取当前按下键盘键的编码
                    //相机绕轴旋转
                    case 87:
                        camera.rotation.z -= 0.5;
                        break;
                    case 83:
                        camera.rotation.z += 0.5;
                        break;
                    case 65:
                        camera.rotation.x -= 0.5;
                        break;
                    case 68:
                        camera.rotation.x += 0.5;
                        break;
                }
            }

        }
        function animate() {
            renderer.render(scene, camera);
            requestAnimationFrame(animate);
        }
        //开始动画
        animate()
    </script>
</body>

</html>